import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { _manageStudent, _delStudent,_allRoom ,_allGrade} from '../api/student';
export interface IndexModelState {
  allStudent: [];
  allRoom:[]
  allGrade:[],
  // allSelect:[]
}

export interface IndexModelType {
  namespace: 'student';
  state: IndexModelState;
  effects: {
    manageStudent: Effect;
    deleteStudent: Effect;
    getallRoom: Effect;
    getallGrade: Effect;
    // setSelect: Effect;
  };
  reducers: {
    ALL_STUDENT: ImmerReducer<IndexModelState>;
    ALL_ROOM: ImmerReducer<IndexModelState>;
    ALL_GRADE: ImmerReducer<IndexModelState>;
    // ALL_SELECT: ImmerReducer<IndexModelState>;
    
  };
 
}

const IndexModel: IndexModelType = {
  namespace: 'student',

  state: {
    allStudent: [],
    allRoom: [],
    allGrade:[],
    // allSelect:[]
  },
  reducers: {
    ALL_STUDENT(state: any, { action }: any) {
      console.log(action);

      return {
        ...state,
        allStudent: [...action],
      };
    },
    ALL_ROOM(state: any, { action }: any) {
      console.log(action);

      return {
        ...state,
        allRoom: [...action],
      };
    },
    ALL_GRADE(state: any, { action }: any) {
      console.log(action);

      return {
        ...state,
        allGrade: [...action],
      };
    },
    // ALL_SELECT(state: any, { action }: any) {
    //   console.log(action);

    //   return {
    //     ...state,
    //     allSelect: [...action],
    //   };
    // },

  
  },

  effects: {
    *manageStudent({ action }, { call, put }) {
      console.log(action,654789);
      
      let result = yield call(_manageStudent);
      // console.log(result,"所有学生");
      
      if (result.data.code == 1) {
        yield put({ type: 'ALL_STUDENT', action: action != null?action:result.data.data });
      }
    },
    *deleteStudent({ action }, { call, put }) {

      let result = yield call(_delStudent, action);
      // console.log(result);
      //如果删除成功   重新发起数据请求
      if (result.data.code == 1) {
        let result = yield call(_manageStudent);
        if (result.data.code == 1) {
          yield put({ type: 'ALL_STUDENT', action: result.data.data });
        }
      }
    },
    *getallRoom({ action }, { call, put }) {

      let result = yield call(_allRoom, action);
      // console.log(result,"教室");
      
      if (result.data.code == 1) {
        yield put({ type: 'ALL_ROOM', action: result.data.data })
      }
    },
    *getallGrade({ action }, { call, put }) {

      let result = yield call(_allGrade, action);
      // console.log(result,"班级号");
      
      if (result.data.code == 1) {
        yield put({ type: 'ALL_GRADE', action: result.data.data })
      }
    },
    //搜索数据
    // *setSelect({ action }, { call, put }) {
    //   console.log(action,888);
    //     yield put({ type: 'ALL_SELECT', action })
      
    // },
    
  },
};

export default IndexModel;
